package com.tjcm.tjcminspectmanager.manager;

import android.content.Context;
import android.content.SharedPreferences;
import cn.emagsoftware.util.CryptoUtilities;
import cn.emagsoftware.util.StringUtilities;

public final class SPManager
{

    public static final String SP_NAME                  = "SPManager";
    public static final String SP_KEY_USERNAME          = "USERNAME";
    public static final String SP_KEY_PASSWORD          = "PASSWORD";
    public static final String SP_KEY_REMEMBER_PASSWORD = "REMEMBER_PASSWORD";
    public static final String SP_KEY_UPLOADTASK_QUEUE  = "UPLOADTASK_QUEUE";
    public static final String SP_KEY_LOGIN_URL         = "LOGIN_URL";
    public static final String SP_KEY_UPLOAD_URL        = "UPLOAD_URL";

    private SPManager()
    {
    }

    public static String getUsername(Context context, String defValue)
    {
        SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        return sp.getString(SP_KEY_USERNAME, defValue);
    }

    public static String getPassword(Context context, String defValue)
    {
        SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        String password = sp.getString(SP_KEY_PASSWORD, null);
        if (password == null)
            return defValue;
        try
        {
            password = new String(CryptoUtilities.decryptByDES(StringUtilities.hexStringToBytes(password), "TJCMInspectManager@Password".getBytes("utf-8")), "utf-8");
        } catch (Exception e)
        {
            throw new RuntimeException(e);
        }
        return password;
    }

    public static boolean isRememberPassword(Context context)
    {
        SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        return sp.getBoolean(SP_KEY_REMEMBER_PASSWORD, false);
    }

    public static void rememberUsername(Context context, String username)
    {
        SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        sp.edit().putString(SP_KEY_USERNAME, username).commit();
    }

    public static void rememberPassword(Context context, String password)
    {
        SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        try
        {
            password = StringUtilities.bytesToHexString(CryptoUtilities.encryptByDES(password.getBytes("utf-8"), "TJCMInspectManager@Password".getBytes("utf-8")));
        } catch (Exception e)
        {
            throw new RuntimeException(e);
        }
        sp.edit().putString(SP_KEY_PASSWORD, password).putBoolean(SP_KEY_REMEMBER_PASSWORD, true).commit();
    }

    public static void clearPassword(Context context)
    {
        SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        sp.edit().remove(SP_KEY_PASSWORD).remove(SP_KEY_REMEMBER_PASSWORD).commit();
    }

    public static String getLoginUrl(Context context, String defValue)
    {
        SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        return sp.getString(SP_KEY_LOGIN_URL, defValue);
    }

    public static String getUploadUrl(Context context, String defValue)
    {
        SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        return sp.getString(SP_KEY_UPLOAD_URL, defValue);
    }

    public static void setLoginUrl(Context context, String loginUrl)
    {
        SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        sp.edit().putString(SP_KEY_LOGIN_URL, loginUrl).commit();
    }

    public static void setUploadUrl(Context context, String uploadUrl)
    {
        SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        sp.edit().putString(SP_KEY_UPLOAD_URL, uploadUrl).commit();
    }

    public synchronized static void offerUploadTaskQueue(Context context, String fileName)
    {
        SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        String queue = sp.getString(SP_KEY_UPLOADTASK_QUEUE, null);
        if (queue != null)
            fileName = queue + "," + fileName;
        sp.edit().putString(SP_KEY_UPLOADTASK_QUEUE, fileName).commit();
    }

    public synchronized static void pollUploadTaskQueue(Context context)
    {
        SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        String queue = sp.getString(SP_KEY_UPLOADTASK_QUEUE, null);
        if (queue != null)
        {
            int index = queue.indexOf(",");
            if (index == -1)
                sp.edit().remove(SP_KEY_UPLOADTASK_QUEUE).commit();
            else
                sp.edit().putString(SP_KEY_UPLOADTASK_QUEUE, queue.substring(index + 1)).commit();
        }
    }

    public static String peekUploadTaskQueue(Context context)
    {
        SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        String queue = sp.getString(SP_KEY_UPLOADTASK_QUEUE, null);
        if (queue == null)
            return null;
        int index = queue.indexOf(",");
        if (index == -1)
            return queue;
        else
            return queue.substring(0, index);
    }

    public static String[] getUploadTaskQueue(Context context)
    {
        SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        String queue = sp.getString(SP_KEY_UPLOADTASK_QUEUE, null);
        if (queue == null)
            return new String[] {};
        return queue.split(",");
    }

}
